import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { viteMockServe } from 'vite-plugin-mock'

export default defineConfig(
  ({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), '')
    // console.log(env)
    return {
      plugins: [vue(),
      viteMockServe({
        supportTs: false,
        logger: false,
        mockPath: "./mock/",
        localEnabled: true // 是否开启开发环境
      })


        , AutoImport({
          imports: ['vue'],
          resolvers: [ElementPlusResolver(), IconsResolver({
            prefix: 'Icon',
          }),]
        }),
      Components({
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            enabledCollections: ['ep'],
          })
        ],
      }),
      Icons({
        autoInstall: true,
      }),
      ],


      resolve: {
        // ↓路径别名，主要是这部分
        alias: {
          "@": path.resolve(__dirname, "src")
        }
      },
      server:{
        host:'0.0.0.0',
        'port':env.VITE_APP_PORT,
        proxy:{
          '/api': {
            target: env.VITE_APP_BASE_URL,
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, ''),
          },
          '^/fallback/.*': {
            target: env.VITE_APP_BASE_URL,
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/fallback/, ''),
          },
        }
      }

    }
  })





